<template>
  <div class="case-parties-container">
    <el-button type="primary" @click="handleAddParty" style="margin-bottom: 10px">
      添加当事人
    </el-button>

    <!-- 调试信息 -->
    <!-- <div v-if="debugMode" class="debug-info">
      <h4>调试信息</h4>
      <pre>{{ JSON.stringify(partyList, null, 2) }}</pre>
    </div> -->

    <div v-if="partyList.length === 0" class="empty-state">
      <el-empty description="暂无当事人信息" />
    </div>

    <div v-else class="parties-list">
      <!-- 委托人身份的卡片放在最前面 -->
      <div
        v-for="party in clientParties"
        :key="party.id"
        class="party-card client-card"
        :class="{
          'individual-card':
            party.partyType === 0 || party.partyType === 1 || party.party_type_name === '个人',
          'enterprise-card': party.partyType === 2 || party.party_type_name === '企业',
          'plaintiff-card': party.relationship_text === '原告',
          'defendant-card': party.relationship_text === '被告',
          'third-party-card': party.relationship_text === '第三人',
        }"
      >
        <div class="party-header">
          <div class="header-left">
            <el-icon
              :size="20"
              :color="
                party.partyType === 0 || party.partyType === 1 || party.party_type_name === '个人'
                  ? '#409eff'
                  : '#67c23a'
              "
              style="flex-shrink: 0"
            >
              <User
                v-if="
                  party.partyType === 0 || party.partyType === 1 || party.party_type_name === '个人'
                "
              />
              <OfficeBuilding v-else />
            </el-icon>
            <h4>{{ party.partyName }}</h4>
          </div>
          <span
            class="relationship-badge"
            :class="{
              'plaintiff-badge': party.relationship_text === '原告',
              'defendant-badge': party.relationship_text === '被告',
              'third-party-badge': party.relationship_text === '第三人',
            }"
          >
            {{ party.relationship_text }}
          </span>
        </div>
        <div class="party-details">
          <div class="detail-item">
            <span class="label">委托身份:</span>
            <span class="value">{{ party.is_client_text }}</span>
          </div>
          <div class="detail-item">
            <span class="label">证件号码:</span>
            <span class="value">{{ party.idNumber }}</span>
          </div>
          <div class="detail-item">
            <span class="label">联系人:</span>
            <span class="value">{{ party.contactPerson }}</span>
          </div>
          <div class="detail-item">
            <span class="label">联系电话:</span>
            <span class="value">{{ party.contactPhone }}</span>
          </div>
        </div>
        <div class="party-actions">
          <el-button size="small" @click="handleEditParty(party)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleRemoveParty(party)">移除</el-button>
        </div>
      </div>

      <!-- 其他当事人 -->
      <div
        v-for="party in nonClientParties"
        :key="party.id"
        class="party-card"
        :class="{
          'individual-card':
            party.partyType === 0 || party.partyType === 1 || party.party_type_name === '个人',
          'enterprise-card': party.partyType === 2 || party.party_type_name === '企业',
          'plaintiff-card': party.relationship_text === '原告',
          'defendant-card': party.relationship_text === '被告',
          'third-party-card': party.relationship_text === '第三人',
        }"
      >
        <div class="party-header">
          <div class="header-left">
            <el-icon
              :size="20"
              :color="
                party.partyType === 0 || party.partyType === 1 || party.party_type_name === '个人'
                  ? '#409eff'
                  : '#67c23a'
              "
              style="flex-shrink: 0"
            >
              <User
                v-if="
                  party.partyType === 0 || party.partyType === 1 || party.party_type_name === '个人'
                "
              />
              <OfficeBuilding v-else />
            </el-icon>
            <h4>{{ party.partyName }}</h4>
          </div>
          <span
            class="relationship-badge"
            :class="{
              'plaintiff-badge': party.relationship_text === '原告',
              'defendant-badge': party.relationship_text === '被告',
              'third-party-badge': party.relationship_text === '第三人',
            }"
          >
            {{ party.relationship_text }}
          </span>
        </div>
        <div class="party-details">
          <div class="detail-item">
            <span class="label">委托身份:</span>
            <span class="value">{{ party.is_client_text }}</span>
          </div>
          <div class="detail-item">
            <span class="label">证件号码:</span>
            <span class="value">{{ party.idNumber }}</span>
          </div>
          <div class="detail-item">
            <span class="label">联系人:</span>
            <span class="value">{{ party.contactPerson }}</span>
          </div>
          <div class="detail-item">
            <span class="label">联系电话:</span>
            <span class="value">{{ party.contactPhone }}</span>
          </div>
        </div>
        <div class="party-actions">
          <el-button size="small" @click="handleEditParty(party)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleRemoveParty(party)">移除</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { computed, ref } from 'vue'
  import { User, OfficeBuilding } from '@element-plus/icons-vue'
  import type { Party } from '@/types'

  const props = defineProps<{
    partyList: Party[]
  }>()

  // 调试模式
  const debugMode = ref(true)

  // 添加调试日志
  console.log('CaseParties组件接收到的partyList:', props.partyList)

  // 添加调试信息
  const emit = defineEmits(['add-party', 'edit-party', 'remove-party'])

  // 计算属性：委托人身份的当事人
  const clientParties = computed(() => {
    console.log('clientParties计算属性执行，partyList:', props.partyList)
    return props.partyList.filter(
      party => party.is_client_text && party.is_client_text === '委托人'
    )
  })

  // 计算属性：非委托人身份的当事人
  const nonClientParties = computed(() => {
    console.log('nonClientParties计算属性执行，partyList:', props.partyList)
    return props.partyList.filter(
      party => !party.is_client_text || party.is_client_text !== '委托人'
    )
  })

  const handleAddParty = () => {
    emit('add-party')
  }

  const handleEditParty = (row: Party) => {
    emit('edit-party', row)
  }

  const handleRemoveParty = (row: Party) => {
    emit('remove-party', row)
  }
</script>

<style scoped>
  .case-parties-container {
    padding: 5px 0;
  }

  .parties-list {
    display: flex;
    flex-direction: column;
    gap: 15px;
  }

  .party-card {
    border: 1px solid #e6e6e6;
    border-radius: 8px;
    padding: 12px;
    background-color: #ffffff;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;
  }

  .party-card:hover {
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
    transform: translateY(-2px);
  }

  /* 委托人卡片样式 - 确保优先级最高 */
  .party-card.client-card {
    border-left: 8px solid #e6a23c !important; /* 橙色边框，加粗 */
    border: 2px solid #f5dab1 !important;
    box-shadow: 0 2px 8px rgba(230, 162, 60, 0.2);
  }

  /* 个人当事人卡片样式 */
  .party-card.individual-card:not(.client-card) {
    border-left: 8px solid #409eff !important; /* 蓝色边框，加粗 */
    border: 2px solid #d9ecff !important;
  }

  /* 企业当事人卡片样式 */
  .party-card.enterprise-card:not(.client-card) {
    border-left: 8px solid #67c23a !important; /* 绿色边框，加粗 */
    border: 2px solid #e1f3d8 !important;
  }

  /* 原告卡片样式 */
  .party-card.plaintiff-card {
    box-shadow: 0 2px 10px rgba(64, 158, 255, 0.2);
  }
  .party-card.plaintiff-card .party-header {
    background-color: #f2f6fc; /* 浅蓝色背景 */
    border-bottom: 1px solid #d9ecff;
    padding: 8px 10px;
  }

  /* 被告卡片样式 */
  .party-card.defendant-card {
    box-shadow: 0 2px 10px rgba(245, 108, 108, 0.2);
  }
  .party-card.defendant-card .party-header {
    background-color: #fef0f0; /* 浅红色背景 */
    border-bottom: 1px solid #fbc4c4;
    padding: 8px 10px;
  }

  /* 第三人卡片样式 */
  .party-card.third-party-card {
    box-shadow: 0 2px 10px rgba(103, 194, 58, 0.2);
  }
  .party-card.third-party-card .party-header {
    background-color: #f0f9eb; /* 浅绿色背景 */
    border-bottom: 1px solid #e1f3d8;
    padding: 8px 10px;
  }

  .party-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    padding: 8px 10px;
    border-bottom: 1px solid #eee;
    border-radius: 6px 6px 0 0;
    transition: background-color 0.3s ease;
    gap: 8px;
  }

  .header-left {
    display: flex;
    align-items: center;
    gap: 8px;
    max-width: 75%;
    overflow: hidden;
    flex: 1;
    min-width: 0; /* 确保flex子元素可以正确收缩 */
  }

  .relationship-badge {
    font-size: 12px;
    padding: 3px 10px;
    border-radius: 12px;
    font-weight: 500;
    white-space: nowrap;
    flex-shrink: 0;
  }

  .plaintiff-badge {
    background-color: #ecf5ff;
    color: #409eff;
    border: 1px solid #d9ecff;
  }

  .defendant-badge {
    background-color: #fef0f0;
    color: #f56c6c;
    border: 1px solid #fbc4c4;
  }

  .third-party-badge {
    background-color: #f0f9eb;
    color: #67c23a;
    border: 1px solid #e1f3d8;
  }

  .party-header h4 {
    margin: 0;
    font-size: 16px;
    color: #333;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0; /* 确保文本可以正确收缩 */
  }

  .party-type {
    font-size: 12px;
    color: #909399;
    background-color: #f0f2f5;
    padding: 2px 6px;
    border-radius: 4px;
  }

  .party-details {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 12px;
  }

  .detail-item {
    display: flex;
    justify-content: space-between;
    font-size: 13px;
  }

  .detail-item .label {
    color: #606266;
    font-weight: 400;
    width: 80px;
    flex-shrink: 0;
  }

  .detail-item .value {
    color: #303133;
    font-weight: 500;
    text-align: right;
    flex: 1;
  }

  .party-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding-top: 8px;
    border-top: 1px solid #eee;
  }

  .empty-state {
    padding: 40px 0;
    text-align: center;
  }
  /* 调试信息样式 */
  .debug-info {
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 16px;
    margin-bottom: 20px;
    background-color: #f9f9f9;
    max-height: 500px;
    overflow-y: auto;
  }

  .debug-info h4 {
    margin-top: 0;
    margin-bottom: 12px;
    color: #333;
    font-size: 16px;
  }

  .debug-info pre {
    margin: 0;
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
    font-size: 13px;
    line-height: 1.5;
    color: #333;
    background-color: #fff;
    padding: 12px;
    border-radius: 4px;
    border: 1px solid #ddd;
  }
</style>
